import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;

import omero.ServerError;
import omero.client;
import omero.api.ServiceFactoryPrx;
import omero.model.Annotation;
import omero.model.FileAnnotation;
import omero.model.FileAnnotationI;
import omero.model.IObject;
import omero.model.Image;
import omero.model.ImageI;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import cli.OmeroServer;
import cli.FileOperations;

import pojos.AnnotationData;
import pojos.FileAnnotationData;
import pojos.ImageData;


public class Annotations {
	
	private static client unsecureClient = null;
	private static ServiceFactoryPrx session = null;
	private static ImageData image;
	
	private static String host = "192.168.20.108";
	private static String username = "root";
	private static String password = "omero";
	private static int port  = 10000;
	
	@BeforeClass
	public static  void oneTimeSetUp() throws Exception {
		OmeroServer.open( host, port, username, password);
	}

	@AfterClass
	public static void oneTimeTearDown() throws Exception {
		OmeroServer.close();
	}

	@Test
	public void testCreateImage() throws ServerError {
		ServiceFactoryPrx session = OmeroServer.getSession();
		
		Image img = new ImageI();
		img.setName(omero.rtypes.rstring("image1"));
		img.setDescription(omero.rtypes.rstring("descriptionImage1"));
		img.setAcquisitionDate(omero.rtypes.rtime(1000000));
		img = (Image) session.getUpdateService().saveAndReturnObject(img);
		image = new ImageData( img );
	}
	
	@Test
	public void testCreateFileAnnotation() throws ServerError, IOException {
		String filename = "./testing/testData/40X.tif";
		File file = new File( filename );
		String sh1;
		try {
			sh1 = FileOperations.generateSha1( filename );

			OmeroServer.addFileToImage(filename, sh1, image, "test" );

			List<IObject> results = OmeroServer.getAnnotations( image.getId() );
			Iterator<IObject> i = results.iterator();
			Annotation ann;
			String fn = "";
			int cnt = 0;
			while ( i.hasNext() ) {
				IObject annot = i.next();
				if ( annot instanceof FileAnnotation ){
					FileAnnotationData fa = new FileAnnotationData( (FileAnnotation)annot );
					if ( fa.getNameSpace().equals( "test" ) ) {
						fn = fa.getFileName();
						cnt++;
					}
				}
			}
			assertEquals( cnt, 1 );
			assertFalse(  filename.equals( fn)   );
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testDeleteFileannotation() throws ServerError {
		List<IObject> results = OmeroServer.getAnnotations( image.getId() );
		Iterator<IObject> i = results.iterator();
		Annotation ann;
		String fn = "";
		int cnt = 0;
		while ( i.hasNext() ) {
			IObject annot = i.next();
			if ( annot instanceof FileAnnotation ){
				FileAnnotationData fa = new FileAnnotationData( (FileAnnotation)annot );
				if ( fa.getNameSpace().equals( "test" ) ) {
	//				Connection.deleteFileAnnotation( fa, image );
					cnt++;
				}
			}
		}
		assertEquals( countAnnotations( image) , 0 );
		
		
	}
	
	private int countAnnotations(ImageData image ) throws ServerError {
		List<IObject> results = OmeroServer.getAnnotations( image.getId() );
		return results.size();
	}
	
	
}
